<?php
$link = mysql_connect('localhost', 'synergy', 'synergy');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("wordpress");

//global $wpdb;
//$table = $wpdb->prefix."asset";
//$myrows = $wpdb->get_results("SELECT RES_TITLE FROM $table ORDER BY RES_TITLE ASC");


$userID=$_GET["userID"];
$hint="";
$queryProj = "SELECT PROJ_ID FROM wp_projectcontributors WHERE CONTRIBUTOR_ID = $userID";
$resultProj = mysql_query($queryProj);
while ($projrow = mysql_fetch_assoc($resultProj)) {
	//get the q parameter from URL
	$q=$_GET["q"];
	$query = "SELECT RES_TITLE, DESCRIPTION FROM wp_asset WHERE IS_DELETED = '0' AND PROJECT_ID = ".$projrow['PROJ_ID'] ." AND RES_TITLE LIKE '$q%' ORDER BY RES_TITLE ASC";
	$result = mysql_query($query);
	
	if (!$result) {
	    $message  = 'Invalid query: ' . mysql_error() . "\n";
	    $message .= 'Whole query: ' . $query;
	    die($message);
	}
	
	
	//lookup all links from the xml file if length of q>0
	if (strlen($q) > 0) {	
		//foreach ($myrows as $myrow){
		while ($myrow = mysql_fetch_assoc($result)) {
			if (stristr($myrow['RES_TITLE'],$q)) {
			      if ($hint=="") {
				        $hint=$myrow['RES_TITLE']." - ".$myrow['DESCRIPTION'];
				  }
			      else {
				        $hint=$hint."<br />".$myrow['RES_TITLE']." - ".$myrow['DESCRIPTION'];			        
			      }
		    }
			
		}
	}
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "") {
  $response="no suggestion";
}
else {
  $response=$hint;
}

//output the response
echo $response;

?> 